package com.shycloud.mido.member.service.impl;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shycloud.mido.common.core.util.R;
import com.shycloud.mido.member.api.entity.AtvActionMedia;
import com.shycloud.mido.member.api.entity.AtvMediaSpace;
import com.shycloud.mido.member.api.util.Constant;
import com.shycloud.mido.member.api.util.UUIDUtil;
import com.shycloud.mido.member.api.vo.AtvActionMediaReqVO;
import com.shycloud.mido.member.mapper.AtvActionMediaMapper;
import com.shycloud.mido.member.mapper.AtvMediaSpaceMapper;
import com.shycloud.mido.member.service.AtvActionMediaService;
import com.shycloud.mido.organ.api.entity.DiaryEntity;
import com.shycloud.mido.organ.api.entity.WorksEntity;
import com.shycloud.mido.organ.api.feign.RemoteDiaryService;

import lombok.extern.slf4j.Slf4j;

@Service
@Slf4j
public class AtvActionMediaServiceImpl extends ServiceImpl<AtvActionMediaMapper, AtvActionMedia> implements AtvActionMediaService {
	@Autowired
	private AtvMediaSpaceMapper atvMediaSpaceMapper;
	@Autowired
	private RemoteDiaryService remoteDiaryService;
	
	
	@Override
	@Transactional(rollbackFor = Exception.class)
	public boolean syncDiary(AtvActionMediaReqVO vo,DiaryEntity diary) throws Exception {
		 
		 Date now = new Date();
		 /*保存活动相册 业务策划规定必到活动相册*/
		 AtvMediaSpace atvMediaSpace = new AtvMediaSpace();
		 atvMediaSpace.setStatus("0");//未审核
		 atvMediaSpace.setSource("1");//个人日记上传到活动相册
		 atvMediaSpace.setMainActiveId(vo.getMainActiveId());
		 atvMediaSpace.setMediaUrl(vo.getMediaUrl());
		 atvMediaSpace.setType(vo.getType());
		 //通用
		 atvMediaSpace.setDelFlag(Constant.DEL_FLAG);
		 atvMediaSpace.setCreateTime(now);
		 atvMediaSpace.setUpdateTime(now);
		 atvMediaSpace.setCreateUser(vo.getMemberId());
		 atvMediaSpace.setUpdateUser(vo.getMemberId());
		 atvMediaSpaceMapper.insert(atvMediaSpace);
		 /*日记添加*/
		 diary.setIsAtvSpace("1");
		 diary.setAtvSpaceId(atvMediaSpace.getId());
		 R<Integer> r = remoteDiaryService.addDiaryAtv(diary);
		 /*个人相册 */
		 AtvActionMedia po = new AtvActionMedia();
		 BeanUtils.copyProperties(vo, po);
		 po.setId(UUIDUtil.getUUID32());
		 po.setDiaryId(r.getData());
		//通用
		 po.setDelFlag(Constant.DEL_FLAG);
		 po.setCreateTime(now);
		 po.setUpdateTime(now);
		 po.setCreateUser(vo.getMemberId());
		 po.setUpdateUser(vo.getMemberId());
		 super.save(po);
		 return true;
	}
	

	@Override
	public IPage<AtvActionMediaReqVO> pageActionMedia(Page<AtvActionMediaReqVO> page, AtvActionMediaReqVO vo) {
		String status = vo.getStatus();
		if (StringUtils.isNotBlank(status)) {
			List<String> statusList = Arrays.asList(status.split(","));
			vo.setStatusList(statusList);
		}
		return baseMapper.pageActionMedia(page, vo);
	}


	@Override
	public List<AtvActionMediaReqVO> listActionMedia(AtvActionMediaReqVO vo) {
		String status = vo.getStatus();
		if (StringUtils.isNotBlank(status)) {
			List<String> statusList = Arrays.asList(status.split(","));
			vo.setStatusList(statusList);
		}
		return baseMapper.listActionMedia(vo);
	}

}


